﻿using System;
using System.Web;
using System.Windows.Forms;
using System.Collections.Generic;
using WindowsLive.Writer.Api;

namespace DotNetBurner.LiveWriterPlugIn
{
    [WriterPlugin("6a512202-c1e9-465d-8dbe-924c35e55372",
                  "DotNetBurner", PublisherUrl = "http://dotnetburner.com",
                  Description = "DotNetBurner \"burn!\" count plugin. You can optionally enable badges for DotNetKicks, DotNetShoutout and WebDevVote.",
                  HasEditableOptions = true)]
    public class DotNetBurnerPlugIn : HeaderFooterSource
    {
        private Settings _settings;

        public override void Initialize(IProperties options)
        {
            base.Initialize(options);

            _settings = new Settings(options);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (Options form = new Options(_settings))
            {
                form.ShowDialog(dialogOwner);
            }
        }

        public override bool RequiresPermalink
        {
            get
            {
                return true;
            }
        }

        public override string GeneratePreviewHtml(ISmartContent smartContent,
                                                   IPublishingContext publishingContext,
                                                   out Position position)
        {
            return GenerateBadges(publishingContext.PostInfo.Permalink, out position);
        }

        public override string GeneratePublishHtml(IWin32Window dialogOwner,
                                                   ISmartContent smartContent,
                                                   IPublishingContext publishingContext,
                                                   bool publish, out Position position)
        {
            return GenerateBadges(publishingContext.PostInfo.Permalink, out position);
        }

        private string GenerateBadges(string url, out Position position)
        {
            position = Position.Footer;
            string encodedUrl = HttpUtility.UrlEncode(url);

            List<string> badges = new List<string>();

            if (_settings.EnableDotNetBurner)
                badges.Add(GenerateBurnImage(encodedUrl));

            if (_settings.EnableDotNetKicks)
                badges.Add(GenerateKickItImage(encodedUrl));

            if (_settings.EnableDotNetShoutOut)
                badges.Add(GenerateShoutItImage(encodedUrl));

            if (_settings.EnableWebDevVote)
                badges.Add(GenerateWebDevVoteImage(encodedUrl));

            return String.Join("&nbsp;", badges.ToArray());
        }

        private string GenerateBurnImage(string url)
        {
            string voteImg = String.Format("http://dotnetburner.com/image.axd?url={0}", url);
            string voteUrl = String.Format("http://dotnetburner.com/vote?url={0}", url);

            return String.Format("<a href=\"{0}\" rev=\"vote-for\"><img src=\"{1}\" style=\"border:0px\" alt=\"Burn!\"/></a>",
                                 voteUrl, voteImg);
        }

        private string GenerateShoutItImage(string url)
        {
            string voteImg = String.Format("http://dotnetshoutout.com/image.axd?url={0}", url);
            string voteUrl = String.Format("http://dotnetshoutout.com/Submit?url={0}", url);

            return String.Format("<a rev=\"vote-for\" href=\"{0}\"><img alt=\"Shout it\" src=\"{1}\" style=\"border:0px\"/></a>",
                                 voteUrl, voteImg);
        }

        private string GenerateKickItImage(string url)
        {
            string voteImg = String.Format("http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url={0}", url);
            string voteUrl = String.Format("http://www.dotnetkicks.com/kick/?url={0}", url);


            return String.Format("<a href=\"{0}\" rev=\"vote-for\"><img src=\"{1}\" style=\"border:0px\" alt=\"kick it\" /></a>",
                                 voteUrl, voteImg);
        }

        private string GenerateWebDevVoteImage(string url)
        {
            string voteImg = String.Format("http://webdevvote.com/Services/Images/KickItImageGenerator.ashx?url={0}", url);
            string voteUrl = String.Format("http://webdevvote.com/vote/?url={0}", url);


            return String.Format("<a href=\"{0}\" rev=\"vote-for\"><img src=\"{1}\" style=\"border:0px\" alt=\"vote it on WebDevVote.com\" /></a>",
                                 voteUrl, voteImg);
        }
    }
}


